﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace book_management_system
{
    public partial class Billing : Form
    {
        public Billing()
        {
            InitializeComponent();
            populate();
        }

        SqlConnection Con = new SqlConnection(@"Data Source=Kami;Initial Catalog=WhiteBookShopDb;Integrated Security=True");//连接或打开
        private void populate()
        {
            Con.Open();//打开数据库
            string query = "select * from BookTb1";
            SqlDataAdapter sda = new SqlDataAdapter(query, Con);
            SqlCommandBuilder builder = new SqlCommandBuilder(sda);//SqlDataAdapter常与SqlCommandBuilder配合使用进行批量操作
            var ds = new DataSet();
            sda.Fill(ds);
            BookDGV.DataSource = ds.Tables[0];
            Con.Close();//关闭数据库
        }

        private void UpdateBook()
        {
            int newQty = stock - Convert.ToInt32(QtyTb.Text);
            try
            {
                Con.Open();//打开数据库
                string query = "update BookTb1 set BQty=" + newQty + "where BId=" + key + "";
                SqlCommand cmd = new SqlCommand(query, Con);
                cmd.ExecuteNonQuery();
                //MessageBox.Show("加入购物车完成!!");//弹出消息框
                Con.Close();//关闭数据库
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message);
            }
            populate();//刷新书列表
        }

        int n = 0,GrdTotal = 0;
        private void AddtoBillBtn_Click(object sender, EventArgs e)
        {
            if(QtyTb.Text == "" || Convert.ToInt32(QtyTb.Text) > stock)
            {
                MessageBox.Show("库存不足!!!");
            }
            else
            {
                int total = Convert.ToInt32(QtyTb.Text) * Convert.ToInt32(PriceTb.Text);
                DataGridViewRow newRow = new DataGridViewRow();
                newRow.CreateCells(BillDGV);
                newRow.Cells[0].Value = n + 1;
                newRow.Cells[1].Value = BTitleTb.Text;
                newRow.Cells[2].Value = PriceTb.Text;
                newRow.Cells[3].Value = QtyTb.Text;
                newRow.Cells[4].Value = total;
                BillDGV.Rows.Add(newRow);
                n++;
                UpdateBook();//每次添加购物车就刷新书列表数据
                GrdTotal = GrdTotal + total;
                TotalLbl.Text = "总金额:"+GrdTotal+"元";
            }
        }

        int key = 0,stock = 0;//stock实时获取库存量
        private void BookDGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            /* 点击列表的类型即可 */
            BTitleTb.Text = BookDGV.SelectedRows[0].Cells[1].Value.ToString();
            //QtyTb.Text = BookDGV.SelectedRows[0].Cells[4].Value.ToString();
            PriceTb.Text = BookDGV.SelectedRows[0].Cells[5].Value.ToString();
            QtyTb.Text = "";//每次点击表格内容就清空输入框的数量
            if (BTitleTb.Text == "")
            {
                key = 0;
                stock = 0;
            }
            else
            {
                key = Convert.ToInt32(BookDGV.SelectedRows[0].Cells[0].Value.ToString());
                stock = Convert.ToInt32(BookDGV.SelectedRows[0].Cells[4].Value.ToString());//赋值库存量给stock
            }
        }

        private void PrintBtn_Click(object sender, EventArgs e)//打印结算
        {
            if (BillDGV.Rows[0].Cells[0].Value == null)
            {
                MessageBox.Show("您还没有添加书籍到购物车!!!");
            }
            else
            {
                /* 录入数据库 */
                try
                {
                    Con.Open();//打开数据库
                    string query = "insert into BillTb1 values('" + Login.UserName + "'," + GrdTotal + ")";//录入该用户的选购数据
                    SqlCommand cmd = new SqlCommand(query, Con);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("订单信息保存成功!!");
                    Con.Close();//关闭数据库
                }
                catch (Exception Ex)
                {
                    MessageBox.Show(Ex.Message);
                }
                /* 打印小票 */
                printDocument1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("pprnm", 285, 600);
                if (printPreviewDialog1.ShowDialog() == DialogResult.OK)
                {
                    printDocument1.Print();
                }
                /* 刷新列表数据 */
                populate();//重新提取数据库数据
                Reset();//清空输入框的内容
            }

            //printDocument1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("pprnm", 285, 600);
            //if (printPreviewDialog1.ShowDialog() == DialogResult.OK)
            //{
            //    printDocument1.Print();
            //}
        }

        int prodid, prodqty, prodprice, tottal, pos=60;

        private void Backlab_Click(object sender, EventArgs e)//退出标签
        {
            Login obj = new Login();
            obj.Show();
            this.Hide();
        }

        private void Billing_Load(object sender, EventArgs e)//界面加载事件
        {
            UserNameLbl.Text = "你好!"+ Login.UserName;//左上角显示登录的用户名
        }

        private void ExitBtn_Click(object sender, EventArgs e)//退出按钮事件
        {
            Application.Exit();
        }



        string prodname;
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //绘制打印内容
            e.Graphics.DrawString("你好书店", new Font("幼圆", 8, FontStyle.Bold), Brushes.Red, new Point(80));
            e.Graphics.DrawString("编号  产品     价格 数量 总计",new Font("幼圆",10,FontStyle.Bold),Brushes.Red,new Point(26,40));
            foreach(DataGridViewRow row in BillDGV.Rows)
            {

                prodid = Convert.ToInt32(row.Cells["Column7"].Value);
                prodname = "" + row.Cells["Column8"].Value;
                prodprice = Convert.ToInt32(row.Cells["Column9"].Value);
                prodqty = Convert.ToInt32(row.Cells["Column10"].Value);
                tottal = Convert.ToInt32(row.Cells["Column11"].Value);
                e.Graphics.DrawString("" + prodid, new Font("幼圆", 8, FontStyle.Bold), Brushes.Blue, new Point(26, pos));
                e.Graphics.DrawString("" + prodname, new Font("幼圆", 8, FontStyle.Bold), Brushes.Blue, new Point(60, pos));
                e.Graphics.DrawString("" + prodprice, new Font("幼圆", 8, FontStyle.Bold), Brushes.Blue, new Point(120+20, pos));
                e.Graphics.DrawString("" + prodqty, new Font("幼圆", 8, FontStyle.Bold), Brushes.Blue, new Point(135+40, pos));
                e.Graphics.DrawString("" + tottal, new Font("幼圆", 8, FontStyle.Bold), Brushes.Blue, new Point(170+40, pos));
                pos = pos + 20;
            }
            e.Graphics.DrawString("订单总额：元" + GrdTotal, new Font("幼圆", 12, FontStyle.Bold), Brushes.Crimson, new Point(60,pos+60));
            e.Graphics.DrawString("***********你好书店***********" , new Font("幼圆", 10, FontStyle.Bold), Brushes.Crimson, new Point(40 ,pos + 85));
            BillDGV.Rows.Clear();//小票打印后清空购物车
            BillDGV.Refresh();
            pos = 100;
            GrdTotal = 0;
        }

        

        private void Reset()//清空输入框
        {
            BTitleTb.Text = "";
            QtyTb.Text = "";
            PriceTb.Text = "";
        }
        private void ResetBtn_Click(object sender, EventArgs e)//重置按钮事件
        {
            Reset();
        }
    }
}
